/*
 *      configuration.c
 *      
 *      Copyright 2009 Daniele Ricci <denn86@gmail.com>, Luca Saccagi <luksak86@gmail.com>
 *      
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *      
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *      
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */

#ifndef _IMPL_CONFIG
#define _IMPL_CONFIG

#include "configuration.h"

#ifdef _WIN32
static conf *load_conf(char *argv){
#else
conf *load_conf(char *argv){
#endif
	char *tlsparm,parm_name[100],parm_val[100];
	FILE *conf_file;
	bool localargv=false;
	bool bsrv_ip=false,bsrv_port=false,busr=false,bpw=false,bmy_uri=false;
	bool tls_enable,proceed_on_tls_fail;
	
	c0=(conf *)malloc(sizeof(conf));
	c0->storedir=NULL;
	c0->dynamicArchive=NULL;
	c0->polling_time=1000;
	c0->reallocation_size=700;
	c0->idmef_window=150000;
	c0->reduct_rate=1;
	c0->read_retries=10;
	tls_enable=true;
	proceed_on_tls_fail=false;
	
	if(argv == NULL){
		localargv=true;
		argv=(char*)calloc(sizeof(char),strlen(DEFAULT_CONF_FILE));
		strncpy(argv,DEFAULT_CONF_FILE,strlen(DEFAULT_CONF_FILE));
	} 
	if((conf_file=fopen(argv,"rt"))==NULL){
		fprintf(stderr,"ERR - Cannot open %s configuration file!\n",argv);
		exit(-1);
	}

	/* Ricerca dei parametri di configurazione nel file */
	while(!feof(conf_file)){
		fscanf(conf_file,"%s = %99[^\n]",parm_name,parm_val);  
		/*"usr = ..."*/
		if(strncasecmp(parm_name,"usr",strlen(parm_name))==0){
			c0->usr=(char *)calloc(sizeof(char),strlen(parm_val)+1);
			busr=true;
			strncpy(c0->usr,parm_val,strlen(parm_val)+1);
		}
		/*"pw = ..."*/
		if(strncasecmp(parm_name,"pw",strlen(parm_name))==0){
			c0->pw=(char *)calloc(sizeof(char),strlen(parm_val)+1);
			bpw=true;
			strncpy(c0->pw,parm_val,strlen(parm_val)+1);		
		}
		/*"srv_ip = ..."*/
		if(strncasecmp(parm_name,"srv_ip",strlen(parm_name))==0){
			bsrv_ip=true;
			c0->srv_ip=(char *)calloc(sizeof(char),strlen(parm_val)+1);
			strncpy(c0->srv_ip,parm_val,strlen(parm_val)+1);
		}
		/*"srv_port = ..."*/
		if(strncasecmp(parm_name,"srv_port",strlen(parm_name))==0){
			c0->srv_port=(char *)calloc(sizeof(char),strlen(parm_val)+1);
			bsrv_port=true;
			strncpy(c0->srv_port,parm_val,strlen(parm_val)+1);
		}
		/*"directory_tmp = .."*/
		if(strncasecmp(parm_name,"directory_tmp",strlen(parm_name))==0){
			c0->dynamicArchive=(char *)calloc(sizeof(char),strlen(parm_val)+1);
			strncpy(c0->dynamicArchive,parm_val,strlen(parm_val)+1);
		}
		/*"directory_store = .."*/
		if(strncasecmp(parm_name,"directory_store",strlen(parm_name))==0){
			c0->storedir=(char *)calloc(sizeof(char),strlen(parm_val)+1);
			strncpy(c0->storedir,parm_val,strlen(parm_val)+1);
		}
		/*"certificate_file = .."*/
		if(strncasecmp(parm_name,"certificate_file",strlen(parm_name))==0){
			c0->certificate=(char *)calloc(sizeof(char),strlen(parm_val)+1);
			strncpy(c0->certificate,parm_val,strlen(parm_val)+1);
		}
		/*"certificate_key = .."*/
		if(strncasecmp(parm_name,"certificate_key",strlen(parm_name))==0){
			c0->certificate_key=(char *)calloc(sizeof(char),strlen(parm_val)+1);
			strncpy(c0->certificate_key,parm_val,strlen(parm_val)+1);
		}
		/*"reallocation_size = .."*/
		if(strncasecmp(parm_name,"reallocation_size",strlen(parm_name))==0){
			sscanf(parm_val,"%d",&c0->reallocation_size);
		}
		/*"polling_time = .."*/
		if(strncasecmp(parm_name,"polling_time",strlen(parm_name))==0){
			sscanf(parm_val,"%d",&c0->polling_time);
		}
		/*"idmef_window = .."*/
		if(strncasecmp(parm_name,"idmef_window",strlen(parm_name))==0){
			sscanf(parm_val,"%d",&c0->idmef_window);
		}
		/*"read_retries = .."*/
		if(strncasecmp(parm_name,"read_retries",strlen(parm_name))==0){
			sscanf(parm_val,"%d",&c0->read_retries);
		}
		/*"reduct_rate = .."*/
		if(strncasecmp(parm_name,"reduct_rate",strlen(parm_name))==0){
			sscanf(parm_val,"%d",&c0->reduct_rate);
		}
		/*"tls_enable = .."*/
		if(strncasecmp(parm_name,"tls_enable",strlen(parm_name))==0){
			tlsparm=(char *)calloc(sizeof(char),strlen(parm_val)+1);
			strncpy(tlsparm,parm_val,strlen(parm_val)+1);
			if(strncasecmp(tlsparm,"true",strlen(tlsparm))!=0){
				tls_enable=false;				
			}
			free(tlsparm);
			tlsparm=NULL;
		}
	}

	fclose(conf_file);
	if(!(bsrv_ip && bsrv_port && busr && bpw)){
		printf("ERR - Config file error\n");
		exit(-1);
	}
	if(c0->storedir == NULL){
		c0->storedir=(char *)calloc(sizeof(char),strlen(STOREDIR)+1);
		strncpy(c0->storedir,STOREDIR,strlen(STOREDIR)+1);
	}
	if(c0->dynamicArchive == NULL){
		c0->dynamicArchive=(char *)calloc(sizeof(char),strlen(DYNDIR)+1);
		strncpy(c0->dynamicArchive,DYNDIR,strlen(DYNDIR)+1);
	}
	if(localargv){ free(argv); argv=NULL;}
	
	return c0;
}



#endif